clear all

* Set your directory path here
local root "D:\Understanding_Bank_Payouts"

capture log close
log using "`root'\Code\Logs\Final_code_RCFS\Figure1.log", replace
display "$S_TIME  $S_DATE"

set more off
set varabbrev off, perm

local finalfile_yearly "`root'\Data\Final\Finalfile_yearly.dta" 

capture noisily mkdir "`root'\Results"
capture noisily mkdir "`root'\Results\Final_code_RCFS"
cd "`root'\Results\Final_code_RCFS"

ssc install tabout

use `finalfile_yearly'

* Figure 1, Balanced panel 

sort instkey year

keep if year > 2000 & year < 2013
bys instkey: egen countdiv = count(divpsh)
bys instkey: egen countrep = count(commstockrep)
bys instkey: egen countout = count(commsharesout)
keep if countdiv == 12 & countrep == 12 & countout == 12

gen divtotal = divpsh*commsharesout
label variable divtotal "Total amount of dividends paid" 

rename commstockrep reptotal
label variable divtotal "Total amount of repurchases" 

drop if neverpayer == 1

keep divtotal reptotal avg_totequY avg_marketcapY instkey instname year
dups instkey instname year, t drop 
drop _expand

foreach var in divtotal reptotal avg_totequWY avg_marketcapY  {
	tabout year using "F1_Balanced_panel_TPO.csv", sum cells(sum `var') style(csv) f(8) h3("Sum of `var'") append
	}

exit, clear
